#include <settings.hpp>
#include <utils.hpp>
//
#include <remoter.hpp>
#include <remoter_cali_pitch_front.hpp>

RemoterCaliPitchFront::RemoterCaliPitchFront(int state)
	: RemoterCali(state)
{
}

RemoterCaliPitchFront::~RemoterCaliPitchFront()
{
}

void RemoterCaliPitchFront::Process(msg_rc_raw_s* rcRaw, msg_rc_s* rc)
{
	RemoterCali::Process(rcRaw, rc);

	if (m_Step == 0)
	{
		memcpy(&m_RcRawBegin, rcRaw, sizeof(msg_rc_raw_s));
	}

	for (int i = 0; i < MSG_RC_E_CNT; i++)
	{
		m_RcRawCurr[i] = FilterLowPass((double)rcRaw->rc_raw[i], m_RcRawPrev[i], m_CaliFilter);
	}

	if (m_Tk % m_CaliWaitTk == 0)
	{
		m_Step++;
		LOG("RemoterCalibration: PITCH FRONT. %d\n", m_Step);
	}

	if (m_Step >= m_CaliWaitStep)
	{
		uint8_t channel = 0;
		uint16_t value	= 0;
		for (int i = 0; i < MSG_RC_E_CNT; i++)
		{
			m_RcRawEnd.rc_raw[i] = m_RcRawCurr[i] + 0.5;
		}
		CalcValueChangedChannel(channel, value, &m_RcRawBegin, &m_RcRawEnd);

		g_Settings->rc_ind_pitch	  = channel;
		g_Settings->rc_cali_pitch_max = value;

		LOG("Pitch channel %d max value %d\n", channel, value);
		Remoter::Instance()->SwitchCaliNext();
	}
}